<?php
namespace app\index\controller;
use app\index\model\CrmCustomer;
use app\index\model\Goods;
use app\index\model\GoodsNext;
use think\Db;
use think\Request;
use think\Controller;
use app\index\model\WxTuwen;
use think\Loader;
use app\app\controller\Subscription;
class Wx extends Base
{
    public function index()
    {
        $query = input();
        $query['rows'] = isset($query['rows'])?$query['rows']:10;
        $query['page'] = isset($query['page'])?$query['page']:1;
        $where['state']=['in',[1,2]];
        if(isset($query['title'])){
            $where['title']=['LIKE',"%".$query['title']."%"];
        }
        if(isset($query['starttime']) && isset($query['endtime'])){
            if($query['starttime']>$query['endtime'])apiReturn(400,'查询开始日期不能大于结束日期');
            if (!empty($query['starttime']) && !empty($query['endtime'])) {
                $where['update_time']=['between',[strtotime($query['starttime']),strtotime($query['endtime'])]];
            }
        }
        $tuWenM = new WxTuwen;
        $list = $tuWenM->where($where)->limit($query['rows'])->page($query['page'])->order('id desc')->select();
        $total = $tuWenM->where($where)->count();
        apiReturn(200,'成功',['list'=>$list,'total'=>$total]);
    }

    public function add(){
        $data=input();
        $validate = Loader::validate('Wx');
        if(!$validate->scene('wx_record')->check($data))apiReturn(400,$validate->getError());
        $data['state']=1;
        $data['create_time']=  $data['update_time'] = time();
        if((new WxTuwen())->allowField(true)->insert($data)) apiReturn(200,'成功',$data);
        apiReturn(400,'失败');
    }

    public function alter(){
        $data=input();
        $validate = Loader::validate('Wx');
        if(!$validate->scene('wx_up')->check($data))apiReturn(400,$validate->getError());
        $data['state']=1;
        $data['update_time']=time();
        if((new WxTuwen())->allowField(['title','desc','img','url','state','update_time'])->update($data)); apiReturn(200,'成功',$data);
        apiReturn(400,'失败');
    }

    public function wx_qr_code(){
        (new Subscription())->getqrcodeticket();
    }

    public function wx_info()
    {
        $data = input();
        if (empty($data['id'])) {
            apiReturn(201,'id为空');
        }
        $wx_info = db('wx_tuwen')->where('id',$data['id'])->find();
        apiReturn(200,'获取成功',$wx_info);
    }

    public function push(){
        $type = input('type');
        if($type=='public'){
            $goods_id = (new Goods())->where('search_type','in',[1,2,3])->column('id');
            $this->push_custome($goods_id);
            apiReturn(200,'公开课推送');
        }else{
            $goods_id = (new Goods())->where('search_type','in',[4,5,6])->column('id');
            $this->push_custome($goods_id);
            apiReturn(200,'正式课推送');
        }
    }

    public function push_custome($goods_id){
        dump($goods_id);die;
        $GoodsNextM =  new GoodsNext();
        $six_start = time();//strtotime(date('Y-m-d').'23:59:59');
        $six_end = strtotime(date('Y-m-d').'23:59:59');//$six_start+86400;
        $where['kaibo_time']=['between',[$six_start,$six_end]];
//        $goodsNext   = $GoodsNextM->where('goods_id','in',$goods_id)->where($where)->order('kaibo_time asc')->select();
        $goods_id = [135];
        $goodsNext   = $GoodsNextM->where('goods_id','in',$goods_id)->order('kaibo_time asc')->select();
        if(count($goodsNext)<=0){
            apiReturn(200,'今天无开课；开始：'.$six_start.'结束：'.$six_end);
        }
        $custome = (new CrmCustomer())->alias('c')->join('user_course u','c.phone=u.phone')->join('goods g','c.public_yingqi=g.id')->where('c.public_yingqi','in',array_column($goodsNext,'goods_id'))->where("u.openid is not null or u.openid <> ''")->field('u.id,u.openid,u.phone,c.public_yingqi,g.title,g.pic1,g.name as teacher_name,g.teacher')->select();
        foreach ($custome as $value){
            foreach ($goodsNext as $val){
                if($value['public_yingqi']==$val['goods_id']){
                    try {
                        $ti = explode('--',explode(' ',$val['desc'])[2])[0];
                        list($hours, $minutes) = explode(':', $ti);
                        $val['kaibo_time']=date('Y-m-d H:i:s', $val['kaibo_time']+($hours * 3600 + $minutes * 60));
                        $moban['title'] =$val['title'];
                        $moban['kaibo_time'] =$val['kaibo_time'];
                        //dump($value['openid']);die;
                        (new Subscription())->send_template($moban,'o99cf5tPAPr2M6QG7PH9q3e_pCTM');dump('测试完毕');die;
//                         (new Subscription())->send_template($val,$value['openid']);
                    }catch (Exception $e){
                        apiReturn(201,$e->getMessage().'开播时间格式错误');
                    }
                }
            }
        }
    }

}
